from django.shortcuts import render, redirect
from utils import wenxin_api as wx
from allModel import models
from allModel.models import user
from allModel.models import Author
from django.http import HttpResponse
from django.contrib.auth import authenticate, login

# 注册（添加user表的数据）
def register(request):
    if request.method == "POST":
        username = request.POST.get("username")
        password = request.POST.get("password", None)
        usercode = request.POST.get("usercode", None)
        twz = models.user.objects.create(username=username, password=password, usercode=usercode)
        twz.save()
    return render(request,"user/register.html")

# 显示数据库表数据列表
def showUser(request):
    user_list = models.user.objects.all()
    return render(request, "user/showUser.html", {"userList": user_list})

# 登录成功进入页面
def index(request):
    return render(request, 'user/index.html')

# 登录功能
def login(request):
    username = request.POST.get("username")
    password = request.POST.get("password")
    Author = authenticate(username=username, password=password)
    print(username, password, type(username))
    print('txt')
    if username == 't' and password == '123':
        return index(request)    # 登录成功返回index页面
    return render(request, 'user/login.html')

def wenxin(request):
    message = request.GET.get("message")
    print(message)
    response = wx.send_chat_req(message)
    result = response.json()
    res = result['result']
    return HttpResponse(res, content_type='application/javascript')



